package com.lin.csdn.controller;

import com.lin.csdn.model.po.Messege;
import com.lin.csdn.service.impl.UserService;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

    /**
     *
     * @param req  传入用户输入的邮箱和密码 ：email,password
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 登录事务
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        Messege loginUser;
        //获取req数据
        String email=req.getParameter("email");
        String password=req.getParameter("password");

        UserService loginService=new UserService();
        loginUser=loginService.login(email,password);
        PrintWriter writer = resp.getWriter(); //响应流
        JSONObject object=new JSONObject();
        if(loginUser.getCode()==200) {
            if(loginUser.getResult().equals("管理员登陆成功")){
                object.put("status","管理员");
            }
            object.put("name",loginUser.getMessage());
        }
        else {
            object.put("msg",loginUser.getResult());
        }
        object.put("code",loginUser.getCode());
        writer.print(object);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         doGet(req, resp);
    }
}
